<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="crm.util.ParamUtil"%>
<%@page import="crm.control.OfferControl"%>
<%@page import="crm.model.Offer"%>
<%@page import="crm.model.Bargain"%>
<%@page import="com.jspsmart.upload.SmartUpload"%>
<%@page import="com.jspsmart.upload.File"%>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Locale"%>
<%@page import="crm.control.BargainControl"%>
<%@page import="crm.model.Statistics"%>
<%@page import="crm.util.DateTime"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../../resources/css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../resources/css/inner.css" type="text/css" media="screen" />
<style type='text/css'>
 a{font-size:12px; text-decoration:none; color</td><td>#70B300;}
</style>
<title>Insert title here</title>
 <script type="text/javascript" src="../../../resources/js/WebCalendar.js"></script>
 <link rel="stylesheet" type="text/css" href="../../../resources/css/jquery-check.css" />
		<script type="text/javascript" src="../../../resources/js/jquery-1.2.6.js"></script>
		<script type="text/javascript" src="../../../resources/js/jquery-check.js"></script>
</head>
<link type="text/css"
		href="../../../resources/css/jquery-ui-1.8.16.custom.css"
		rel="stylesheet" />
	<script type="text/javascript"
		src="../../../resources/js/jquery-1.6.2.min.js"></script>
	<script type="text/javascript"
		src="../../../resources/js/jquery-ui-1.8.16.custom.min.js"></script>
    <script type="text/javascript">
    $(function(){
             $("#bargainId").keyup(function(){
                  if($("#bargainId").val()!=null){
                      $.ajax({
                         type : "get",
                         cache : false,
                         url : "bargainId_is_exist.jsp",
                         dataType : "html",
                         data : {
                         bargainId : $("#bargainId").val()
                         },
                         success : function(data){
                            if(data == 0){
                            	$("#bargainId")
                            	.addClass('right-style')
                            	$("#tip").css('color','#76B700')
                            	.html('该合同编号可以使用');
x                            } else {
                            	$("#bargainId")
                            	.removeClass('right-style') 
                   				.addClass('error-style')
                   				$("#tip").css('color','#C6533C')
                            	.html('该合同编号已存在');
                            }
                         }
                      })
                  }
                  else{return false;}
             })
    })
    </script>
<body>
<%
                  int offerId = ParamUtil.getInt(request,"id",0);
                  Offer offer = OfferControl.getOfferById(offerId);
                  String action=ParamUtil.getString(request,"action","");
                  if(action==null||action.equals("")){

%>
<br/>
转入合同之前请完善一下信息：
<br/>
<br/>
<form action="turn_to_bargain.jsp?action=todo&id=<%=offerId %>" method="post"  enctype="multipart/form-data">
<table>
<tr><td>合同编号</td><td><input type="text" name="bargainId" id="bargainId"/><span id='tip'></span></td></tr>
<tr><td>合同标题 </td><td><input type="text" name="title"></td></tr>
<tr><td>最晚回款日期</td><td><input type="text" name="latest_date"  maxlength="20"  onclick="SelectDate(this,'yyyy-MM-dd')" readonly  style="width:100px;cursor:pointer"/></td></tr>
<tr><td>关联报价 </td><td><%=offer.getTheme() %></td></tr>
<tr><td>报价金额 </td><td><%=offer.getOffer_money() %>元</td></tr>
<tr><td>成交金额 </td><td><input type="text" name="deal_money" /><select name="danwei_deal"><option value="0" selected>元</option><option value="1">万元</option></select></td></tr>
<tr><td>相关PDF文件 </td><td><input style='width:60px;' type="file" name="pdf" size="20" jType="file"></td></tr>
<tr><td colspan='2' style='text-align: center;'><input  class='button' type="submit" value="提交">&nbsp;&nbsp;&nbsp;&nbsp;
<input class='button' type="reset" value="重置">&nbsp;&nbsp;&nbsp;&nbsp;
<input class='button' type="button" onclick="javascript:window.history.go(-1);" value="返回"></td></tr>
</table>
</form>
<%}else{
	          

                String filePath = "/resources/resources";
	            String fileName="";
	            SmartUpload upload = new SmartUpload();
	            upload.initialize(config, request, response);
	            //upload.setMaxFileSize(2048 * 1024);
	 		   //upload.setAllowedFilesList("jpg,gif,png,jpeg,bmp,txt,doc,pdf,exe,zip,rar,xls");
	 		   try{
	 			   upload.upload();
	 			   File uploatFile = upload.getFiles().getFile(0);
	 			  if(!uploatFile.isMissing()){
			 			   Date currentTime = new Date();
			 			   //System.out.print(currentTime);
			 			   SimpleDateFormat date = new SimpleDateFormat("yyyyMMddhhmmssSS",Locale.US);
			 			   fileName = new String(date.format(currentTime).getBytes("UTF-8"));
			 			   String ext = uploatFile.getFileExt();   //获得文件后缀
			 			   fileName = fileName+"."+ext;
			 			   System.out.println("NNNN"+fileName+"FFFF");
			 			   uploatFile.saveAs(filePath+"/"+fileName,upload.SAVE_VIRTUAL);
	 			  }
	 		   }catch(Exception e) {
	 			  e.printStackTrace();
				  %><script>alert(文件上传失败：请检查选择的文件是否存在？");</script>
				  <%
			   }
	 		    Bargain ba = new Bargain();
	 		    ba.setBargain_id(upload.getRequest().getParameter("bargainId"));
		        ba.setTitle(new String(upload.getRequest().getParameter("title").getBytes(),"GBK"));
		        System.out.println(new String(upload.getRequest().getParameter("title").getBytes("ISO-8859-1"),"utf-8"));
			    ba.setOffer_id(offerId);
			    ba.setLatest_date(upload.getRequest().getParameter("latest_date"));
			    ba.setIs_overdue("0");
			    ba.setOverdue_reason("暂无");
			    String deal_money = upload.getRequest().getParameter("deal_money");
			    if(upload.getRequest().getParameter("danwei_deal").equals("1")&&deal_money!=null){
			    	deal_money = (String.valueOf(Float.parseFloat(deal_money)*10000));
			    }
			    ba.setDeal_money(deal_money);
			   // String returned_money =  upload.getRequest().getParameter("returned_money");
			    //if( upload.getRequest().getParameter("danwei_returned").equals("1")&&returned_money!=null){
			    	//returned_money = (String.valueOf(Float.parseFloat(returned_money)*10000));
			   // }
			   ba.setReturned_money("0");
	 		   ba.setPdf(fileName);
	 		   
	            if(BargainControl.addBargain(ba)){
	            	%>
	            	<script>
	            	alert("转入成功")
	            	window.top.location.href="../bargain/show_all_bargains.jsp";
	            	</script>
	            	<%
	            }else{
	            	%>
	            	<script>
	            	alert("转入失败")
	            	history.go(-1);
	            	</script>
	            	<%
	            }
	 		   
	 		   
	
}%>

</body>
</html>